package com.jarvis.pzz.modules.collectionshop;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.jarvis.pzz.R;
import com.jarvis.pzz.common.BaseActivity;
import com.jarvis.pzz.common.ResponseData;
import com.jarvis.pzz.models.ShopListModel;
import com.jarvis.pzz.models.ShopsListByFilterModel;
import com.jarvis.pzz.modules.shop.adapter.ShopAdapter;
import com.jarvis.pzz.modules.shop.view.ShopDetailsActivity;
import com.jarvis.pzz.seervers.RequestService;
import com.jarvis.pzz.util.DividerItemDecoration;
import com.jarvis.pzz.util.net.RequestApi;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrDefaultHandler2;
import in.srain.cube.views.ptr.PtrFrameLayout;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class CollectionShopActivity extends BaseActivity implements View.OnClickListener{
    @BindView(R.id.rel_left)
    RelativeLayout rel_left;
    @BindView(R.id.lin_title)
    RelativeLayout lin_title;
    @BindView(R.id.tv_title)
    TextView tv_title;

    @BindView(R.id.recy_vc)
    RecyclerView recy_vc;
    @BindView(R.id.ptr_vc)
    PtrClassicFrameLayout ptr_vc;
    private ShopAdapter shopAdapter;
    private RequestService service;

    private int pageSize = 10;//页面数量
    private int currentPage = 1;//当前页面
    private int maxPage = 1;//最大页面

    private List<ShopListModel> shopListModels;


    @Override
    public int getLayoutId() {
        return R.layout.activity_collection_shop;
    }

    @Override
    public void initData() {
        ptr_vc.postDelayed(
                new Runnable() {
                    @Override
                    public void run() {
                        ptr_vc.autoRefresh();
                    }
                }, 100);

    }

    @Override
    public void initView() {
        tv_title.setText("我收藏的铺源");
        service = RequestApi.create(RequestService.class);
        shopListModels = new ArrayList<>();
        setBar();
        setAdapter();
        setRefresh();
        setListener();

    }

    /**
     * 设置监听事件
     */
    public void setListener() {
        rel_left.setOnClickListener(this);
        recy_vc.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void SimpleOnItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                ShopListModel model = shopAdapter.getData().get(i);
                startActivity(new Intent(mActivity, ShopDetailsActivity.class).putExtra("shopId", model.getShopId()));

            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rel_left:
                finish();
                break;
        }
    }

    /**
     * 设置下拉刷新
     */
    public void setRefresh() {
        ptr_vc.setMode(PtrFrameLayout.Mode.BOTH);
        ptr_vc.disableWhenHorizontalMove(true);
        ptr_vc.setLastUpdateTimeRelateObject(this);
        ptr_vc.setPtrHandler(new PtrDefaultHandler2() {

            @Override
            public boolean checkCanDoLoadMore(PtrFrameLayout frame, View content, View footer) {
                return PtrDefaultHandler2.checkContentCanBePulledUp(frame, recy_vc, footer);
            }

            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                return PtrDefaultHandler2.checkContentCanBePulledDown(frame, recy_vc, header);
            }

            @Override
            public void onLoadMoreBegin(PtrFrameLayout frame) {
                if (currentPage < maxPage) {
                    currentPage++;
                    myCollect();
                } else {
                    ptr_vc.refreshComplete();
                }
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                shopAdapter.setNewData(new ArrayList<ShopListModel>());
                currentPage = 1;
                myCollect();
            }
        });
    }


    /**
     * 设置adapter
     */
    /**
     * 设置adapter
     */
    public void setAdapter() {
        shopAdapter = new ShopAdapter(R.layout.item_shop, new ArrayList<ShopListModel>());
        recy_vc.setLayoutManager(new LinearLayoutManager(mActivity));
        recy_vc.addItemDecoration(new DividerItemDecoration(mActivity, DividerItemDecoration.VERTICAL_LIST, getResources().getDrawable(R.drawable.divier_order_gray)));
        shopAdapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_BOTTOM);
        recy_vc.setAdapter(shopAdapter);
    }


    @Override
    public void onResume() {
        super.onResume();
    }


    private void setBar() {
        lin_title.setVisibility(View.VISIBLE);
        lin_title.getBackground().setAlpha(255);
        int statusBarHeight1 = -1;
        //获取status_bar_height资源的ID
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            //根据资源ID获取响应的尺寸值
            statusBarHeight1 = getResources().getDimensionPixelSize(resourceId);
        }
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(lin_title.getLayoutParams());
        lp.setMargins(0, statusBarHeight1, 0, 0);
        lin_title.setLayoutParams(lp);
    }

    private void myCollect() {

        Call<ResponseData<ShopsListByFilterModel>> myCollect = service.myCollect(String.valueOf(currentPage), String.valueOf(pageSize));
        myCollect.enqueue(new Callback<ResponseData<ShopsListByFilterModel>>() {
            @Override
            public void onResponse(Call<ResponseData<ShopsListByFilterModel>> call, Response<ResponseData<ShopsListByFilterModel>> response) {
                ptr_vc.refreshComplete();
                if (response.body() != null) {
                    int total_count = response.body().getTotalcount();
                    int resultCode = response.body().getResultCode();
                    if (resultCode == 200) {
                            ShopsListByFilterModel shopsListByFilterModel = response.body().getData();
                            maxPage = total_count % pageSize == 0 ? total_count / pageSize : total_count / pageSize + 1;
                            shopAdapter.addData(shopsListByFilterModel.getShopList());

                    } else {
                        showTextToast(response.body().getMessage());
                    }
                } else {
                    showTextToast("请求失败");
                }
            }

            @Override
            public void onFailure(Call<ResponseData<ShopsListByFilterModel>> call, Throwable t) {
                ptr_vc.refreshComplete();
                showTextToast(RequestApi.detailError(t));
            }
        });

    }


}

